// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
const db=cloud.database();
exports.main = async (event, context) => {
  try {
    let msgResult = await cloud.openapi.security.msgSecCheck({
      content: event.artcile+event.artcileTitle
    })
    let imageResult = await cloud.openapi.security.imgSecCheck({
      media: {
        header: {
          'Content-Type': 'application/octet-stream'
        },
        contentType: 'image/png',
        value: Buffer.from(event.image)
      }
    })
    if(msgResult.errCode==0 && imageResult.errCode==0){
      let Dates=new Date();
      let day=Dates.getDate() < 10 ? '0'+(Dates.getDate()) : Dates.getDate();
      let month=Dates.getMonth()+1 < 10 ? '0'+(Dates.getMonth()+1) : Dates.getMonth()+1;
      let year=Dates.getFullYear();
      let pushTime=year+"年"+month+"月"+day;
      let timeStamp=new Date().getTime();
      return await db.collection("artcile").add({
        data:{
        artcile:event.artcile,
        image:event.image,
        nickName:event.nickName,
        pushOpenid:event.pushOpenid,
        pushTime:pushTime,
        artcileTitle:event.artcileTitle,
        timeStamp:timeStamp
        }
      })
    }
  } catch (error) {
    console.log(error)
  }
}